package com.zby.charity.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.charity.common.BaseResponse;
import com.zby.charity.common.ErrorCode;
import com.zby.charity.common.PageRequest;
import com.zby.charity.common.ResultUtils;
import com.zby.charity.constant.UserConstant;
import com.zby.charity.entity.ProjectEntity;
import com.zby.charity.entity.UserEntity;
import com.zby.charity.entity.vo.ProjectParams;
import com.zby.charity.exception.BusinessException;
import com.zby.charity.service.ProjectService;
import com.zby.charity.service.UserService;
import com.zby.charity.utils.OssConfig;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author zby
 * @email 2069189112@qq.com
 * @date 2025-01-02 17:53:44
 */

@Slf4j
@RestController
@RequestMapping("charity/project")
public class ProjectController {
    @Autowired
    public ProjectService projectService;
    @Autowired
    public UserService userService;
    @Autowired
    public OssConfig ossConfig;

    @PostMapping("/upload")
    public BaseResponse<Boolean> uploadImage(@RequestParam MultipartFile file, @RequestParam Integer id) {
        if (file.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = ossConfig.uploadImg(file, id);
        return ResultUtils.success(b);
    }

    @PostMapping("/upload1")
    public BaseResponse<String> uploadImagProject(@RequestParam MultipartFile file) {
        if (file.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String url = ossConfig.uploadImg(file);
        return ResultUtils.success(url);
    }
    @PostMapping("/upload2")
    public BaseResponse<String> uploadImagPost(@RequestParam MultipartFile file) {
        if (file.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String url = ossConfig.uploadImg1(file);
        return ResultUtils.success(url);
    }
    @PostMapping("/publishProject")
    public BaseResponse<Boolean> publishProject(@RequestBody ProjectEntity project) {
        if (project == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        String img = project.getImg();
        // 为项目设置封面，如果为空就生成随机封面
        project.setImg(StringUtils.isEmpty(img) ? OssConfig.getRandomImg() : img);
        project.setGmtcreate(new Date());
        project.setGmtmodified(new Date());
        System.out.println(project);
        boolean save = projectService.save(project);
        if (!save)
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        return ResultUtils.success(true);
    }

    @GetMapping("/list")
    public BaseResponse<List<ProjectEntity>> list() {
        List<ProjectEntity> projectEntityList = projectService.list();
        if (projectEntityList == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(projectEntityList);
    }

    @PostMapping("/getProjectByIdAndName")
    public BaseResponse<ProjectEntity> getProjectById(@RequestBody ProjectParams params) {
        String name = params.getName();
        Long id = params.getId();
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (name == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        queryWrapper.eq("id", id);
        ProjectEntity one = projectService.getOne(queryWrapper);
        if (one != null) {
            return ResultUtils.success(one);
        }
        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateProject(@RequestBody ProjectEntity project, HttpServletRequest request) {
        if (project == null || project.getId() == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        UserEntity loginUser = userService.getLoginUser(request);
        if (loginUser == null)
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        System.out.println(project);
        boolean b = projectService.updateById(project);
        if (!b)
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        return ResultUtils.success(true);
    }
}
